package com.numericme.locale.switcher;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import com.numericme.locale.switcher.tools.Tools;

public class MainActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener{
	
	GoogleAnalyticsTracker tracker;
	  
	final Activity activity = this;
	
    private SharedPreferences prefs;

	private Button stopButton;
	private Button startButton;
	private Button settingsButton;
	
    private boolean isNotificationsInvisible;
    private int notificationsIcon=0;

    private String action;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
		String preftoUse = Tools.SHARED_PREFS_NAME;
	    prefs = MainActivity.this.getSharedPreferences(preftoUse, 0);

		// initialize the preferences
		onSharedPreferenceChanged(prefs,Tools.MAIN_ACTIVITY_CREATE);
			    
	    tracker = GoogleAnalyticsTracker.getInstance();	    
	    // Start the tracker in manual dispatch mode...
	    tracker.start("UA-XXXXXXXX-X", this);

		setContentView(R.layout.main);

		stopButton = (Button) findViewById(R.id.stop_button);
		stopButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Tools.cancelNotifications(activity);
				startButton.setVisibility(View.VISIBLE);
				stopButton.setVisibility(View.GONE);
			}
		});
		
		startButton = (Button) findViewById(R.id.start_button);
		startButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Tools.displayNotification(activity, isNotificationsInvisible);
				startButton.setVisibility(View.GONE);
				stopButton.setVisibility(View.VISIBLE);
			}
		});
		
		settingsButton = (Button) findViewById(R.id.settings_button);
		settingsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				launchSettings();
			}
		});

	    action = this.getIntent().getAction();
	    if(action != null) {
			Tools.displayNotification(activity, isNotificationsInvisible);
			startButton.setVisibility(View.GONE);
			stopButton.setVisibility(View.VISIBLE);
		    if (action.equals(Intent.ACTION_BOOT_COMPLETED) && prefs.getBoolean(Tools.AUTO_ACTIVATE_ON_BOOT_KEY, Boolean.parseBoolean(getString(R.string.auto_activate_on_boot_pref_def_value)))){
		    	this.finish();
		    }
	    }

		tracker.trackPageView("/MainActivity");
        // Manually start a dispatch.
		tracker.dispatch();
		
		prefs.registerOnSharedPreferenceChangeListener(this);
		        
		String versionName = Tools.getVersionName(this,MainActivity.class);
		boolean newVersion = !prefs.getString(Tools.LAST_START_VERSION_FLAG_ID, "").equals(versionName);
		if(newVersion){			
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(Tools.LAST_START_VERSION_FLAG_ID, versionName);   	

			showCustomDialog(getString(R.string.pref_change_log_title) + versionName,getString(R.string.change_log_msg) + "\n\n",android.R.drawable.ic_menu_help);
			
			editor.commit();
		}
    }
    
	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
    @Override
	protected void onDestroy() {
		super.onDestroy();

	    // Stop the tracker when it is no longer needed.
	    tracker.stop();
	    
		prefs.unregisterOnSharedPreferenceChangeListener(this);
		
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}
    
	private void launchSettings() {
		tracker.trackPageView("/Settings");
        // Manually start a dispatch.
		tracker.dispatch();
		
    	final Intent settingsIndent = new Intent(MainActivity.this, SettingsActivity.class);
    	startActivityForResult(settingsIndent, 0);
		
	}

	private void showCustomDialog(String title, CharSequence msg, int icon) {
		//Custom alert dialog
		AlertDialog checkInstallDialog;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.custom_dialog,(ViewGroup) findViewById(R.id.layout_root));
		
		TextView text_1 = (TextView) layout.findViewById(R.id.text_1);
		text_1.setText(msg);
		
		builder.setView(layout);			
		builder.setTitle(title);
		builder.setIcon(icon);
		builder.setCancelable(false);
		builder.setPositiveButton(
				getString(R.string.tutorial_dialog_btn_ok),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();						
						MainActivity.this.openOptionsMenu();
					}
				});
		checkInstallDialog = builder.create();
		try {
			checkInstallDialog.show();
		}
		catch(OutOfMemoryError e){
		}		
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String keyChanged) {
		if(keyChanged.equals(Tools.INVISIBLE_NOTIFICATIONS_KEY) || keyChanged.equals(Tools.MAIN_ACTIVITY_CREATE)){
		  	  isNotificationsInvisible = sharedPrefs.getBoolean(Tools.INVISIBLE_NOTIFICATIONS_KEY, Boolean.parseBoolean(getString(R.string.invisible_notifications_pref_def_value)));
		}
	}
}
